// pages/home/home.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isloading: false,
    docData: [],
    type: 'supply',
    startX: 0,
    touchMoveX: 0
  },
  touchstart(e) {
    this.setData({
      startX: e.changedTouches[0].clientX
    })
  },
  touchmove(e) {
    this.setData({
      touchMoveX: e.changedTouches[0].clientX
    })
    //console.log('start '+this.data.startX)
    //console.log('move '+this.data.touchMoveX)
    if (this.data.startX - this.data.touchMoveX > 60 && this.data.type == 'supply') {
      //console.log(this.data.active)
      this.setData({
        type: this.data.type == 'supply' ? 'need' : 'supply'
      })
    } else if (this.data.startX - this.data.touchMoveX < -60 && this.data.type == 'need') {
      this.setData({
        type: this.data.type == 'supply' ? 'need' : 'supply'
      })
    }
  },
  changeType() {
    this.setData({
      type: this.data.type == 'supply' ? 'need' : 'supply'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    app.initlogin().then((res) => {
      this.getDocdata()
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    if (this.data.isloading === false) {
      this.setData({
        docData: [],
      })
      this.getDocdata()
      wx.stopPullDownRefresh()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isloading === false) {
      this.getDocdata()
      console.log(this.data.isloading)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 获取数据
   */
  async getDocdata() {
    wx.showLoading({
      title: '加载中',
    })
    this.data.isloading = true
    const res = await app.get('filelist/' + (Object.keys(this.data.docData).length + 1) + '/', {})
    this.setData({
      docData: this.data.docData.concat(res.data)
    })
    this.data.isloading = false
    wx.hideLoading()
    return 0
  },

  gotoSearch() {
    wx.navigateTo({
      url: '/searchs/search/search',
    })
  },
  gotochoose() {
    wx.navigateTo({
      url: '/searchs/choose/choose',
    })
  },

  onTabItemTap(e) {
    this.setData({
      docData: []
    })
    this.getDocdata()
  }
})